package com.example.myapplication.view;

import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.R;
import com.example.myapplication.view.fragment.MineFragment;
import com.example.myapplication.presenter.MainPresenter;
import com.example.myapplication.view.fragment.CartFragment;
import com.example.myapplication.view.fragment.HomeFragment;
import com.example.myapplication.view.fragment.TypeFragment;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements MainView {
    private static String TAG = "MainActivity";
    private RelativeLayout[] btnCtr;
    private ImageView[] imgs;
    private TextView[] txts;
    private int[] defaultIcon = {R.drawable.home, R.drawable.type, R.drawable.mart, R.drawable.my};
    private int[] selectIcon = {R.drawable.home_s, R.drawable.type_s, R.drawable.mart_s, R.drawable.my_s};
    private MainPresenter mp;
    private int select = 0;
    private List<Fragment> frag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar ab = getSupportActionBar();//添加返回按钮
        if (ab != null) {
            ab.setHomeButtonEnabled(true);
            ab.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        setData();
        mp = new MainPresenter(this);
        mp.setTabInfo(txts[0], Color.GREEN, imgs[0], selectIcon[0]);
        for (int i = 0; i < btnCtr.length; i++) {
            btnCtr[i].setTag(i);
            btnCtr[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int idx = (int) v.getTag();
                    Log.i(TAG, "onClick: " + idx);
                    if (idx == select) {
                        return;
                    }
                    mp.setTabInfo(txts[select], Color.GRAY, imgs[select], defaultIcon[select]);
                    mp.setTabInfo(txts[idx], Color.GREEN, imgs[idx], selectIcon[idx]);
                    select = idx;
                    getSupportFragmentManager().beginTransaction().replace(R.id.framge_ctr, frag.get(idx)).commit();
                }
            });
        }
    }

    //添加返回按钮
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        switch (item.getItemId()) {
//            case android.R.id.home:
//                this.finish(); // back button
//                return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }
    private void initView() {
        btnCtr = new RelativeLayout[]{
                (RelativeLayout) findViewById(R.id.home_ctr),
                (RelativeLayout) findViewById(R.id.type_ctr),
                (RelativeLayout) findViewById(R.id.cart_ctr),
                (RelativeLayout) findViewById(R.id.my_ctr)
        };
        imgs = new ImageView[]{
                (ImageView) findViewById(R.id.home_img),
                (ImageView) findViewById(R.id.type_img),
                (ImageView) findViewById(R.id.cart_img),
                (ImageView) findViewById(R.id.my_img)
        };
        txts = new TextView[]{
                (TextView) findViewById(R.id.home_txt),
                (TextView) findViewById(R.id.type_txt),
                (TextView) findViewById(R.id.cart_txt),
                (TextView) findViewById(R.id.my_txt)
        };
        frag = new ArrayList<Fragment>();
        frag.add(HomeFragment.newInstance());
        frag.add(new TypeFragment());
        frag.add(new CartFragment());
        frag.add(new MineFragment());
        getSupportFragmentManager().beginTransaction().add(R.id.framge_ctr, frag.get(0)).commit();
    }

    private void setData() {
        Log.i("interface", "in setdata: ");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://192.168.3.201:80/interface.php");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    //关闭输入流和输出流
                    in.close();
                    Log.i("interface", "in setdata: " + response);
//                    showResponse(response.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    @Override
    public void setTabInfo(TextView tv, int color, ImageView iv, int id) {
        tv.setTextColor(color);
        iv.setImageResource(id);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        super.getMenuInflater().inflate(R.menu.actionbar_menu, menu);
//        return true;
//    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
